/**
 * 
 */
package weightsimulator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Lasse Poulsen
 *
 */
public class Simulator {
	
	BufferedReader in = null;
	PrintWriter out1 = null;
	Passer out = null;
	
	private class Passer {
		private PrintWriter out;
		public Passer(PrintWriter out) {
			this.out = out;
		}
		public void print(String str) {
			System.err.println("SEND: " + str);
			out.print(str);
			out.flush();
		}
	}
	
	public Simulator() throws IOException {
		ServerSocket server = new ServerSocket(8000);
		Socket client = server.accept();
		
		out1 = new PrintWriter(client.getOutputStream(), true);
		in = new BufferedReader(new InputStreamReader(client.getInputStream()));
		
		out = new Passer(out1);
		
		while (client.isConnected()) {
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				System.err.println(inputLine);
				if (inputLine.startsWith("@")) {
					reset(inputLine);
				} else if (inputLine.startsWith("P120")) {
					p120(inputLine);
				} else if (inputLine.startsWith("P121")) {
					p121(inputLine);
				} else if (inputLine.startsWith("RM20")) {
					rm20(inputLine);
				} else if (inputLine.startsWith("RM30")) {
					rm30(inputLine);
				} else if (inputLine.startsWith("RM39")) {
					rm39(inputLine);
				} else if (inputLine.startsWith("RM49")) {
					rm49(inputLine);
				} else if (inputLine.startsWith("S")) {
					s(inputLine);
				} else if (inputLine.startsWith("T")) {
					t(inputLine);
				} else if (inputLine.startsWith("D")) {
					d(inputLine);
				}
			}
		}
	}
	
	private void t(String inputLine) { // DONE
		out.print("T S      0.062 kg\r\n");
	}

	private void d(String inputLine) { // DONE
		out.print("D A\r\n");
	}
	
	private void s(String inputLine) { // DONE
		out.print("S S    0.438 kg\r\n");
	}

	private void rm49(String inputLine) { // DONE
		out.print("RM49 B\r\n");
		try { Thread.sleep(1500); } catch (InterruptedException e) {}
		out.print("RM49 A 1\r\n");
	}

	private void rm39(String inputLine) { // DONE
		out.print("RM39 A\r\n");
		try { Thread.sleep(1500); } catch (InterruptedException e) {}
		out.print("RM30 A 1\r\n");
	}

	private void rm30(String inputLine) {
		out.print("RM30 B\r\n");
	}

	private void rm20(String inputLine) { // DONE
		if (inputLine.startsWith("RM20 8") || inputLine.startsWith("RM20 10")) {
			out.print("RM20 B\r\n");
			try { Thread.sleep(1500); } catch (InterruptedException e) {}
			out.print("RM20 A \"J\"\r\n");
		} else if (inputLine.startsWith("RM20 3")) {
			out.print("RM20 B\r\n");
			try { Thread.sleep(1500); } catch (InterruptedException e) {}
			out.print("RM20 A \"1\"\r\n");			
		}
	}

	private void p121(String inputLine) { // DONE
		out.print("P121 A\r\n");
	}

	private void p120(String inputLine) { //DONE
		out.print("P120 A\r\n");
	}

	private void reset(String inputLine) {
		out.print("I4 A \"3154307\"\r\n");
	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		new Simulator();
	}

}
